#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/mm_types.h>

static int __init print_vma_info_init(void)
{
    struct task_struct *task;
    struct mm_struct *mm;
    struct vm_area_struct *vma;

    // 获取当前进程的 task_struct
    task = current;

    // 获取当前进程的 mm_struct
    mm = task->mm;
    if (!mm) {
        printk(KERN_INFO "No memory management structure available.\n");
        return -1;
    }

    printk(KERN_INFO "Virtual Memory Areas (VMAs) for process %d:\n", task->pid);

    // 遍历 vm_area_struct 链表
    for (vma = mm->mmap; vma; vma = vma->vm_next) {
        printk(KERN_INFO "VMA: Start=0x%lx, Size=%lu bytes\n",
               vma->vm_start, vma->vm_end - vma->vm_start);
    }

    return 0;
}

static void __exit print_vma_info_exit(void)
{
    printk(KERN_INFO "Module exit.\n");
}

module_init(print_vma_info_init);
module_exit(print_vma_info_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Print virtual memory areas (VMAs) of the current process");
